// pages/productList/productList.js
import ajax from "../../utils/ajax";
import api from "../../utils/api";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasMore: true,
    page: 1,
    searchWord: '',
    dataList: [],
    default: [0,1,2,3,4,5,6,7,8,9]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    wx.setNavigationBarTitle({
      title: '商品',
    })

    this.setData({
      searchWord: options.search
    })

    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getList()
  },

  // 点击搜索
  handleClickSearch() {
    this.setData({
      dataList: [],
      hasMore: true,
      page: 1
    });
    this.getList();
  },

  // 获取列表
  getList () {
    let that = this;
    if (!that.data.hasMore) {
      return;
    }
    wx.showLoading({
      title: '加载中',
    })
    let tData = that.data
    let param = {
      PageIndex: tData.page,
      Search: tData.searchWord
    }
    console.log(param)
    // ajax.ajax(api.productList, param)
    //   .then((val) => {
    //     console.log(val)
    //     if ((val.Tag == 1) && val.Result) {
    //       let data = tData.dataList.concat(val.Result);
    //       let hasMore = (data.length < val.TotalCount);
    //       let page = tData.page + 1 ;
    //       that.setData({
    //         dataList: data,
    //         hasMore,
    //         page
    //       });
    //       wx.hideLoading();
    //     } else {
    //       wx.showToast({title: "加载失败，请重试", icon: 'none'})
    //     }
    //   })
    //   .catch((val) => {
    //     wx.showToast({title: "加载失败，请重试", icon: 'none'})
    //   })

    setTimeout(() => {
      wx.hideLoading()
      let data = tData.dataList.concat(tData.default);;
      let page = tData.page + 1 ;
      that.setData({
        dataList: data,
        hasMore: true,
        page
      });
    }, 1500)
    
  }
})